package com.seecen.controller;

import com.seecen.pojo.dto.PropertyDto;
import com.seecen.service.PropertyService;
import com.seecen.utils.JwtHelper;
import com.seecen.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: SharkBookkeeping-end
 * @description: 资产管理控制器
 * @author: XXW
 * @create: 2025-10-16 14:18
 * @version: 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/property")
@CrossOrigin
public class PropertyController {

    @Autowired
    private PropertyService propertyService;
    
    @Autowired
    private JwtHelper jwtHelper;

    /**
     * 获取当前用户的资产列表
     * @param token 用户token
     * @return 资产列表
     */
    @GetMapping
    public Result getPropertyList(@RequestHeader String token){
        log.info("获取资产列表");
        Long userId = jwtHelper.getUserId(token);
        return propertyService.getPropertyList(userId);
    }

    /**
     * 添加资产
     * @param propertyDto 资产信息
     * @param token 用户token
     * @return 操作结果
     */
    @PostMapping
    public Result addProperty(@RequestBody PropertyDto propertyDto, @RequestHeader String token){
        log.info("添加资产");
        Long userId = jwtHelper.getUserId(token);
        return propertyService.addProperty(propertyDto, userId);
    }

    /**
     * 更新资产
     * @param id 资产ID
     * @param propertyDto 资产信息
     * @param token 用户token
     * @return 操作结果
     */
    @PutMapping("/{id}")
    public Result updateProperty(@PathVariable Long id, @RequestBody PropertyDto propertyDto, @RequestHeader String token){
        log.info("更新资产 - id: {}", id);
        Long userId = jwtHelper.getUserId(token);
        return propertyService.updatePropertyById(id, propertyDto, userId);
    }

    /**
     * 删除资产
     * @param id 资产ID
     * @param token 用户token
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result deleteProperty(@PathVariable Long id, @RequestHeader String token){
        log.info("删除资产 - id: {}", id);
        Long userId = jwtHelper.getUserId(token);
        return propertyService.deletePropertyById(id, userId);
    }
}
















